package m.jclecai.com.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by hp on 2017/4/24.
 */

public class IndicatorAdapter extends FragmentPagerAdapter {
    private String[] TITLE;
    FragmentManager fm;
    private List<Fragment> fragmentList;

    public IndicatorAdapter(FragmentManager fm, String[] TITLE, List<Fragment> fragmentList) {
        super(fm);
        this.TITLE=TITLE;
        this.fm=fm;
        this.fragmentList=fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        Bundle args = new Bundle();
        args.putString("arg", TITLE[position]);
        fragmentList.get(position).setArguments(args);
        return  fragmentList.get(position);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        this.fm.beginTransaction().show(fragment).commit();
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Fragment fragment = fragmentList.get(position);
        fm.beginTransaction().hide(fragment).commit();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLE[position % TITLE.length];
    }

    @Override
    public int getCount() {
        return TITLE.length;
    }
}
